package com.gexiaobao.pigeon.ui.fragment.mine.tools

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.widget.SearchView
import androidx.fragment.app.setFragmentResult
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.amap.api.services.help.Inputtips
import com.amap.api.services.help.InputtipsQuery
import com.amap.api.services.help.Tip
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.Constant
import com.gexiaobao.pigeon.app.util.AMapUtil.IsEmptyOrNullString
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.databinding.FragmentInputTipBinding
import com.gexiaobao.pigeon.ui.adapter.InputTipsAdapter
import com.gexiaobao.pigeon.viewmodel.MatchLineQueryViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 9:36
 * @Describe    :
 */
class FragmentInputTip : BaseFragment<MatchLineQueryViewModel, FragmentInputTipBinding>(),
    SearchView.OnQueryTextListener,
    Inputtips.InputtipsListener {

    private var mSearchView: SearchView? = null// 搜索输入框
    private var mInputListView: RecyclerView? = null// 提示列表
    private var mCurrentTipList: ArrayList<Tip>? = null// 提示列表数据
    private var mInputTipAdapter: InputTipsAdapter? = null // 列表适配器

    override fun initView(savedInstanceState: Bundle?) {
        initSearchView()
        mInputListView = activity?.findViewById(R.id.inputTipList)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.back) {
            when (it) {
                mDatabind.back -> {
                    nav().navigateUp()
                }
            }
        }
    }

    private fun initSearchView() {
        mSearchView = activity?.findViewById<SearchView>(R.id.keyWord)
        mSearchView?.setOnQueryTextListener(this)
        //设置SearchView默认为展开显示
        mSearchView!!.isIconified = false
        mSearchView!!.onActionViewExpanded()
        mSearchView!!.isSubmitButtonEnabled = false
    }

    override fun onQueryTextSubmit(query: String?): Boolean {
        return false
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onQueryTextChange(newText: String?): Boolean {
        if (!IsEmptyOrNullString(newText)) {
            val inputquery = InputtipsQuery(newText, Constant.DEFAULT_CITY)
            val inputTips = Inputtips(context, inputquery)
            inputTips.setInputtipsListener(this)
            inputTips.requestInputtipsAsyn()
        } else {
            if (mInputTipAdapter != null && mCurrentTipList != null) {
                mCurrentTipList!!.clear()
                mInputTipAdapter!!.notifyDataSetChanged()
            }
        }
        return false
    }

    override fun onGetInputtips(tipList: MutableList<Tip>?, rCode: Int) {
        if (rCode == 1000) { // 正确返回
            var hasSnippet = true
            while (hasSnippet) {
                if (tipList != null) {
                    if (tipList[0].point == null) {
                        tipList.removeAt(0)
                    } else {
                        hasSnippet = false
                    }
                }
            }
            mCurrentTipList = tipList as ArrayList<Tip>?
            val listString: ArrayList<String> = ArrayList()
            if (tipList != null) {
                for (i in tipList.indices) {
                    listString.add(tipList[i].name)
                }
            }
            mInputListView!!.layoutManager = LinearLayoutManager(context)
            mInputTipAdapter = InputTipsAdapter(context, mCurrentTipList)
            mInputTipAdapter!!.setOnItemClickListener { view ->
                val position = mInputListView!!.getChildAdapterPosition(view)
                val tip: Tip = mCurrentTipList!![position]
//                val name = mCurrentTipList!![position].name
                setFragmentResult("RESULT_CODE_INPUT_TIPS", Bundle().apply {
                    putParcelable(Constant.EXTRA_TIP, tip)
                })
                nav().navigateUp()
//                addObjectBoxData(name)
            }
            mInputListView!!.adapter = mInputTipAdapter
        } else {
            RxToast.showToast(rCode.toString())
        }
    }
}